package com.wy.projectssj.interceptor;

import com.wy.projectssj.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: wy
 * @Date:2019/8/30 11:00
 */
public class PrivilegeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                         //从作用域获得user对象
         if(handler instanceof DefaultServletHttpRequestHandler){
             return  true;
         }
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            //放行
            return  true;
        }else {
             response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }
    }
}
